Code
library(tidyverse)
library(lubridate)
library(nycflights13)
package for date manipulation
Tony Duan
August 11, 2023
input as character
convert into date type with as.Date()
convert into date type with ymd()
get today with today()
get local timezone
make multiple column character to date with make_date()
# A tibble: 336,776 × 6
year month day hour minute departure
<int> <int> <int> <dbl> <dbl> <date>
1 2013 1 1 5 15 2013-01-01
2 2013 1 1 5 29 2013-01-01
3 2013 1 1 5 40 2013-01-01
4 2013 1 1 5 45 2013-01-01
5 2013 1 1 6 0 2013-01-01
6 2013 1 1 5 58 2013-01-01
7 2013 1 1 6 0 2013-01-01
8 2013 1 1 6 0 2013-01-01
9 2013 1 1 6 0 2013-01-01
10 2013 1 1 6 0 2013-01-01
# ℹ 336,766 more rows
# A tibble: 336,776 × 6
year month day hour minute departure
<int> <int> <int> <dbl> <dbl> <dttm>
1 2013 1 1 5 15 2013-01-01 05:15:00
2 2013 1 1 5 29 2013-01-01 05:29:00
3 2013 1 1 5 40 2013-01-01 05:40:00
4 2013 1 1 5 45 2013-01-01 05:45:00
5 2013 1 1 6 0 2013-01-01 06:00:00
6 2013 1 1 5 58 2013-01-01 05:58:00
7 2013 1 1 6 0 2013-01-01 06:00:00
8 2013 1 1 6 0 2013-01-01 06:00:00
9 2013 1 1 6 0 2013-01-01 06:00:00
10 2013 1 1 6 0 2013-01-01 06:00:00
# ℹ 336,766 more rows
using interval()
find two dates gap
find day gap
find month gap
find year gap
get year
get month
get date of the month
get date of the year
get date of the week
get hour
get minute
get second
https://r4ds.had.co.nz/dates-and-times.html
---
title: "Handling date with R package:[lubridate]"
subtitle: "package for date manipulation"
author: "Tony Duan"
date: "2023-08-11"
categories: [packages]
execute:
warning: false
error: false
format:
html:
toc: true
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
{width="286"}
# date format
```{r}
library(tidyverse)
library(lubridate)
library(nycflights13)
```
input as character
```{r}
date1='2023-01-01'
class(date1)
```
convert into date type with `as.Date()`
```{r}
date2=as.Date('2023-01-01')
class(date2)
```
convert into date type with `ymd()`
```{r}
date3=ymd('2023-01-01')
class(date3)
```
get today with `today()`
```{r}
today()
```
get local timezone
```{r}
Sys.timezone()
```
# change date format
make multiple column character to date with `make_date()`
```{r}
flights %>%
select(year, month, day, hour, minute) %>%
mutate(departure = make_date(year, month, day))
```
```{r}
flights %>%
select(year, month, day, hour, minute) %>%
mutate(departure = make_datetime(year, month, day, hour, minute))
```
# day differnce between two dates
```{r}
day1=ymd('2022-01-01')
day2=ymd('2023-02-03')
diff=day2-day1
```
```{r}
diff
```
using `interval()` find two dates gap
```{r}
interval(day1,day2) %>% as.period()
```
find day gap
```{r}
interval(day1,day2)%/% days(1)
```
find month gap
```{r}
interval(day1,day2)%/% months(1)
```
find year gap
```{r}
interval(day1,day2)%/% years(1)
```
# day and time
```{r}
now_time=now()
now_time
```
get year
```{r}
year(now_time)
```
get month
```{r}
month(now_time)
```
get date of the month
```{r}
mday(now_time)
```
get date of the year
```{r}
yday(now_time)
```
get date of the week
```{r}
wday(now_time)
```
get hour
```{r}
hour(now_time)
```
get minute
```{r}
minute(now_time)
```
get second
```{r}
second(now_time)
```
# Reference
https://r4ds.had.co.nz/dates-and-times.html